package com.example.demo.application_event_publisher;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @EventListener注解监听事件
 */
@Component
@Slf4j
public class GlobalEventListener {

    @EventListener(classes = UserRegisterEvent.class, condition = "#event.user.age >= 18")
    public void onUserGrant1(UserRegisterEvent event) {
        User user = event.getUser();
        try {
            Thread.sleep(9000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("用户[" + user.getName() + "]注册成功，年龄[" + user.getAge() + "]>=18，授予成人权限 延时9秒");
    }

    @EventListener(classes = UserRegisterEvent.class, condition = "#event.user.age < 18")
    public void onUserGrant2(UserRegisterEvent event) {
        try {
            Thread.sleep(9000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        User user = event.getUser();
        log.info("用户[" + user.getName() + "]注册成功，年龄[" + user.getAge() + "]<18，授予未成年权限 延时9秒");
    }}